package com.zhongxiangspace.controller.pagemanage;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.gson.Gson;

import com.zhongxiangspace.entity.BuildingsAttribute;
import com.zhongxiangspace.entity.County;
import com.zhongxiangspace.entity.OfficeBuildings;
import com.zhongxiangspace.entity.QueryAnalyzer;
import com.zhongxiangspace.result.*;
import com.zhongxiangspace.service.CountyService;
import com.zhongxiangspace.service.OfficeBuildingService;
import com.zhongxiangspace.tools.DateUtil;
import com.zhongxiangspace.tools.FileUtil;
import com.zhongxiangspace.tools.ReadProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 办公楼
 */
@Controller
public class OfficeBuildingsController {
    @Autowired
    private ReadProperties readProperties;
    @Autowired
    private CountyService countyService;
    @Autowired
    private OfficeBuildingService officeBuildingService;


    /**
     * 楼盘属性
     */
    @ResponseBody
    @PostMapping("/officeBuilding/building/updataAttr.do")
    public Result updataAttr(BuildingsAttribute b) {
            b.setModify_time(DateUtil.getDateTime());
        BuildingsAttribute buildingsAttribute = this.officeBuildingService.queryAttr(b.getBuild_id());
        if (buildingsAttribute != null) {
            b.setId(buildingsAttribute.getId());
           boolean flag=this.officeBuildingService.updataAttr(b);
            return new NormalResult(flag);
        } else {
            b.setCreate_time(DateUtil.getDateTime());
            boolean flag=this.officeBuildingService.insertAttr(b);
            return new NormalResult(flag);
        }
    }

    /**
     * 楼盘属性
     */
    @ResponseBody
    @PostMapping("/officeBuilding/building/queryAttr.do")
    public Result queryAttr(Integer building_id) {
        BuildingsAttribute buildingsAttribute = this.officeBuildingService.queryAttr(building_id);
        if (buildingsAttribute != null) {
            return new NormalResult(buildingsAttribute);
        } else {
            return new NormalResult("error");
        }
    }

    /**
     * 添加
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/officeBuilding/building/updata.do")
    public Result updata(OfficeBuildings officeBuildings) {
        OfficeBuildings office = this.officeBuildingService.queryOne(officeBuildings.getBuilding_id());
        office.setModify_time(DateUtil.getDateTime());
        office.setBuilding_name(officeBuildings.getBuilding_name());
        office.setBuilding_address(officeBuildings.getBuilding_address());
        office.setCoordinate(officeBuildings.getCoordinate());
        office.setContext_img_path(officeBuildings.getContext_img_path());
        office.setCover_img_path(officeBuildings.getCover_img_path());
        office.setCounty_id(officeBuildings.getCounty_id());
        office.setTrade_area_id(officeBuildings.getTrade_area_id());
        boolean flag = this.officeBuildingService.updata(office);
        return new NormalResult("success");
    }

    /**
     * 更改
     *
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/officeBuilding/building/edit.do")
    public String edit(HttpServletRequest request, Model model, Integer id) {
        List<County> tradeAreaList = countyService.queryAllType(null);
        model.addAttribute("data", tradeAreaList);
        OfficeBuildings officeBuildings = this.officeBuildingService.queryOne(id);
        model.addAttribute("object", officeBuildings);
        return "basics/officeBuildings/buildings_updata";
    }


    @RequestMapping("/officeBuilding/building/list.do")
    public String list(HttpServletRequest request, Model model) {
        List<County> tradeAreaList = countyService.queryAllType(null);
        model.addAttribute("data", tradeAreaList);
        String gson = new Gson().toJson(tradeAreaList);
        model.addAttribute("dataJson", gson);
        return "basics/officeBuildings/buildings_list";
    }

    /**
     * 异步获取 data
     *
     * @param queryAnalyzer
     * @return
     */
    @ResponseBody
    @GetMapping("/officeBuilding/building/ajaxList.do")
    public Response ajaxList(QueryAnalyzer queryAnalyzer) {
        PageHelper.startPage(queryAnalyzer.getPage(), queryAnalyzer.getLimit(), "modify_time DESC ");
        Page<OfficeBuildings> officeBuildingsList = (Page<OfficeBuildings>) this.officeBuildingService.queryByQueryAnalyzer(queryAnalyzer);
        Response result = new Response(0, "", officeBuildingsList.getTotal(), officeBuildingsList.getResult());
        return result;
    }


    /**
     * 异步热门和非热门
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/officeBuilding/building/status-hot.do")
    public Response statusHot(Integer id) {
        int i = 1;
        if (id != null) {
            OfficeBuildings officeBuildings = this.officeBuildingService.queryOne(id);
            if (officeBuildings != null && officeBuildings.getHotCode().equals(0)) {
                officeBuildings.setHotCode(1);
                boolean flag = this.officeBuildingService.updataHotCode(officeBuildings);
            } else if (officeBuildings != null && officeBuildings.getHotCode().equals(1)) {
                i = 0;
                officeBuildings.setHotCode(0);
                boolean flag = this.officeBuildingService.updataHotCode(officeBuildings);
            }
        }
        Response result = new Response(0, "success", 0, null);
        return result;
    }

    /**
     * 异步删除 data
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/officeBuilding/building/delete.do")
    public Response delete(Integer id) {
        if (id != null) {
            boolean flag = this.officeBuildingService.delete(id);
        }
        Response result = new Response(0, "success", 0, null);
        return result;
    }


    /**
     * 异步修改上下线 data
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/officeBuilding/building/status.do")
    public Response status(Integer id) {
        if (id != null) {
            OfficeBuildings officeBuildings = this.officeBuildingService.queryOne(id);
            if (officeBuildings != null && officeBuildings.getShowCode().equals(0)) {
                officeBuildings.setShowCode(1);
                boolean flag = this.officeBuildingService.updataStatusCode(officeBuildings);
            } else if (officeBuildings != null && officeBuildings.getShowCode().equals(1)) {
                officeBuildings.setShowCode(0);
                boolean flag = this.officeBuildingService.updataStatusCode(officeBuildings);
            }
        }
        Response result = new Response(0, "success", 0, null);
        return result;
    }


    /**
     * 获取所有区域
     *
     * @param request
     * @param model
     * @return
     */
    @GetMapping("/officeBuilding/building/add.do")
    public String add(HttpServletRequest request, Model model) {
        List<County> tradeAreaList = countyService.queryAllType(0);
        model.addAttribute("data", tradeAreaList);
        return "basics/officeBuildings/buildings_add";
    }

    /**
     * 获取所有区域
     *
     * @param request
     * @param model
     * @return
     */
    @ResponseBody
    @PostMapping("/officeBuilding/building/ajaxTradeArea.do")
    public Result ajaxTradeArea(HttpServletRequest request, Model model, Integer id) {
        List<County> tradeAreaList = countyService.queryAllType(id);
        return new NormalResult(tradeAreaList);
    }


    /**
     * 添加
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/officeBuilding/building/insert.do")
    public Result insert(OfficeBuildings officeBuildings) {
        officeBuildings.setModify_time(DateUtil.getDateTime());
        officeBuildings.setCreate_time(DateUtil.getDateTime());
        boolean flag = this.officeBuildingService.insert(officeBuildings);
        return new NormalResult("success");
    }
    /**
     * 上传图片
     *
     * @param multipartFile
     * @return
     */
    @ResponseBody
    @PostMapping("/officeBuilding/building/upload.do")
    public Result upload(@RequestParam("file") MultipartFile multipartFile) {
        Map<String, Object> map = null;
        try {
            if (!multipartFile.isEmpty()) {
                map = FileUtil.saveFile(multipartFile, "cover", readProperties.getImagePath());
            }
        } catch (Exception e) {
            return new ExceptionResult(e);
        }
        return new NormalResult(map);
    }
}
